from abc import ABC, abstractmethod

from chernc.treediff.gumtree.matchers.configurable import Configurable
from chernc.treediff.gumtree.matchers.mapping_store import MappingStore
from chernc.treediff.gumtree.tree.tree import TreeLike


class Matcher(Configurable):
    """
    Interface for matchers that produce mappings between the nodes of a src and dst AST.
    """

    @abstractmethod
    def match(self, src: TreeLike, dst: TreeLike, mappings: MappingStore) -> MappingStore:
        """
        Compute and return the mappings between a provided src and dst AST.
        The mappings are added as a side effect in the provided mapping store.
        The provided mapping store is returned as well to allow chaining.
        """
        pass

    def match_with_new_store(self, src: TreeLike, dst: TreeLike) -> MappingStore:
        """
        Utility method that computes and returns the mappings between a provided
        src and dst AST in a new mapping store.
        """
        return self.match(src, dst, MappingStore(src, dst))